#!/bin/bash

OK=0
WARN=1
CRITICAL=2
UNKNOWN=3
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
#####################
#######################
c_host=`hostname -s`
c_eth=eth4     ################
c_statlog=/tmp/volstatlog
#c_ip=`grep IPADDR /etc/sysconfig/network-scripts/ifcfg-$c_eth|awk -F = '{print $2}'`
c_ip=`ifconfig |grep "inet addr:192.168.50"|awk '{print $2}'|awk -F ':' '{print $2}'`
Judge=0
Ok_name=''
Fail_name=''
#echo $c_host $c_i

gfsmount=''

if [ -f /etc/init.d/glusterd ];then
        gfsmount="besfs2"
elif [ -f /etc/init.d/zecloudd ];then
        gfsmount="ybjgfs"
else
        echo "glusterfs isnot installed!"
        exit 2

fi


for choice in $gfsmount                    ###
do
        case $choice in
        ybjgfs)
		D_zfs=`/etc/init.d/zecloudd status |awk '{print $2}'`

		if [ "$D_zfs" != "(pid" ]
		then
        		echo "zecloud daemon is stopped or wrong"
        	exit $CRITICAL
		fi
		
		echo "" > $c_statlog
		for i in `ls /etc/zecloudd/vols/`
		do
			STATUS=`grep status /etc/zecloudd/vols/$i/info |awk -F '=' '{print $2}'`
			if [ $STATUS == 1 ]
			then
			#echo vol $i start
			cat /etc/zecloudd/vols/$i/info >> $c_statlog
			fi
		done
		;;
	besfs2)
		D_zfs=`/etc/init.d/glusterd status |awk '{print $2}'`

		if [ "$D_zfs" != "(pid" ]
		then
        		echo "glusterd daemon is stopped or wrong"
        		exit $CRITICAL
		fi

		echo "" > $c_statlog
		for i in `ls /var/lib/glusterd/vols/`
		do
        		STATUS=`grep status /var/lib/glusterd/vols/$i/info |awk -F '=' '{print $2}'`
        		if [ $STATUS == 1 ]
        		then
                		#echo vol $i start
               	 		cat /var/lib/glusterd/vols/$i/info >> $c_statlog
        		fi
		done
		;;
	esac

done
###

#####################################
for i in `grep -E "$c_ip|$c_host" $c_statlog |awk -F '-' '{print "/"$(NF-1)"/"$NF}'`
do
	#echo $i
	H_num=`ps -ef |grep $i|grep -v grep|wc -l`
	#echo $H_num
	if (($H_num==0)) 
	then
		Judge=1
		Fail_name="${Fail_name}device:${i} "
	elif (($H_num==1))
	then
		Ok_name="${Ok_name}device:${i} "
	fi
done

rm -f $c_statlog
#echo "Fail_name $Fail_name Ok_name $Ok_name"
if [ $Judge -eq 1 ]
then
	echo "$Fail_name wrong"
	exit $CRITICAL
else
	echo "$Ok_name are OK"
	exit $OK
fi
